<?php

class Contact_us extends MY_Controller {
    
    function __construct() {
        parent::__construct();
    }
	
    public function index() {
		
        $this->load->library('recaptcha');
        /* set a template variable */
        $data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();

        /* set the view to be loaded inside the basic 'template' view */
        $data['template'] = 'contact';
        
        $this->load->view('template', $data);	
    }


 public function submit() {
        //Load REcaptcha again.
        $this->load->library('recaptcha');

        //Call to recaptcha to get the data validation set within the class. 
        $this->recaptcha->recaptcha_check_answer();

        //TODO: Store the message to database

			
			
		if ($this->recaptcha->getIsValid()) 
		{
			$this->load->library('user_agent');
			$title->input->post('name');
			//$email->input->post('email');
			//$phone->input->post('phone');
			$content->input->post('message');
			$to_user_id = $this->input->post('to_user');
			
			$message = new Message();
            $user = new User();
			
			$user->where('id', $to_user_id)->get();
			$message->author_id = $this->session->userdata('user_id');
			$message->title = $title;
			$message->content = $content;
			if ($message->save($user)) {
				$this->session->set_flashdata('flashMessage', 'Private message successfully sent!');
				$this->session->set_flashdata('flashMessageType', 'success');
			} else {
				$this->session->set_flashdata('flashMessage', 'Private message was not sent. Try again or contact admin');
				$this->session->set_flashdata('flashMessageType', 'danger');
			}
			 redirect($this->agent->referrer());
        } 
		else
		{
            $this->session->set_flashdata('error','incorrect captcha'); 
			$this->load->library('recaptcha');
			/* set a template variable */
			$data['recaptcha_html'] = $this->recaptcha->recaptcha_get_html();

			/* set the view to be loaded inside the basic 'template' view */
			$data['template'] = 'contact';
        
			$this->load->view('template', $data);	
        }
    }
}

/* End of file contact.php */
/* Location: ./application/controllers/contact.php */